package com.diandian.backend.dingding.exchange.controller;

import com.diandian.backend.dingding.exchange.service.DingdingTokenService;
import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.base.boot.starter.response.BaseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/token")
public class DingdingTokenController {

    private static final Logger logger = LoggerFactory.getLogger(DingdingTokenController.class);

    @Autowired
    private DingdingTokenService dingdingTokenService;

    /**
     * 获取token
     */
    @GetMapping("/get")
    public BaseResult getToken(@RequestHeader(value = "appKey") String appKey,
                                  @RequestHeader(value = "appSecret") String appSecret) throws Exception {
        logger.info("收到钉钉获取token请求，appKey：{}, appSecret: {}", appKey, appSecret);

        String token = dingdingTokenService.getToken(appKey, appSecret);
        if(token == null || "".equals(token)){
            throw new DefaultException("未获取到令牌数据");
        }

        return new BaseResult(0, "success", token);
    }
}
